//index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showIcon: true,
    screenHeight: app.globalData.screenHeight,
    modalName: '',
    scrollTop: 0,
    menuList: [{
      to: 'compiler',
      text: '舔狗日记'
    }, {
      to: 'compiler',
      text: '舔狗日记'
    }],
  },

  //路由跳转
  toRouter: function(e) {
    let toName = e.currentTarget.dataset.to
    console.log();
    wx.navigateTo({
      url: '/pages/'+toName+'/'+toName+'',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.cloudLoad();
  },

  // 数据库
  cloudLoad: () => {
    const SMALL_TOOL_DB = wx.cloud.database({
      env: 'smalltool-5ezhy'
    })
    SMALL_TOOL_DB.collection('cdn').add({
        // data 字段表示需新增的 JSON 数据
        data: {
          description: "learn cloud database",
          due: new Date("2018-09-01"),
          tags: [
            "cloud",
            "database"
          ],
          location: new SMALL_TOOL_DB.Geo.Point(113, 23),
          done: false
        }
      })
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.error(err);
      })
  },
  toNetease() {
    wx.navigateTo({
      url: '/pages/compiler/compiler',
    })
  },
  showModal(e) {
    this.setData({
      modalName: 'viewModal'
    })
  },
  hideModal(e) {
    this.setData({
      modalName: ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  scroll(e) {
    let that = this;
    let sHeight = Number((that.data.screenHeight / 2) - 100);
    let sTop = e.detail.scrollTop > sHeight ? sHeight : Number(e.detail.scrollTop);
    if (sTop > sHeight) return;
    that.setData({
      scrollTop: sTop / sHeight
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})